
<?php 
  require_once("includes/config.php"); 
  require_once("includes/validation.php");
  
  error_reporting(0);
    if($_POST["submit"]=="Send") {
	 $email = make_safe($_POST["email"]);
    function createRandomPassword() {
    $chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
    $i = 0;
    $pass = '' ;

    while ($i <= 8) {
        $num = mt_rand(0,61);
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
    }

    $pw = createRandomPassword();
	$new_pass = sha1($pw);
	$rows= query("SELECT * FROM registration WHERE Email =? ",$email);
$user_info["name"] = $rows[0]["user_name"];

if(empty($user_info["name"])){ 
   $mess= "هذا البريد الاليكترونى غير موجود لدينا";
    }
else{
    $update= query("UPDATE registration SET password=? WHERE Email=?",$new_pass,$email);


$subject = 'Password Reset';

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
        $bound;

    $message .=
  '

        <BODY BGCOLOR="white">
        <body>

        </br>
        <div style=" height="40" align="left">

        <font size="3" color="red" style="text-decoration:none;font-family:Lato light">
        <div class="info" Style="align:left;">

        <p>your user name is:  '.$user_info["name"].' </p> 
        
		  <p>your password is:   '.$pw.'</p> 
         

                        </div>
        </br>
        <p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
        </font>
        </div>
        </body>
    '."\n\n".
                                                                $bound_last;
													
	mail($email, $subject, $message);
	
  $mess = "تم ارسال رساله الى بريد الاليكترونى";
   }
  
	}
	render("forgot.php", array("title" => "نسيت كلمة المرور","mess"=>$mess));
   ?>